﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace BoxPcTool
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private List<Protocol> _Protocols = new List<Protocol> { Protocol.TCP, Protocol.UDP, Protocol.ANY };

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      Protocol_ComboBox.ItemsSource = _Protocols;
      Protocol_ComboBox.SelectedIndex = 0;
    }

    private void AddRol_Click(object sender, RoutedEventArgs e)
    {
      Message_TextBlock.Text = null;
      try
      {
        string name = RolName_TextBox.Text;
        var protocol = Protocol_ComboBox.SelectedItem as Protocol?;
        if (protocol == null) throw new ArgumentNullException("必须选择协议类型。");
        int port = Int32.Parse(RolPort_TextBox.Text);
        if (String.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("必须填写规则的名称。");

        FireWallHelper.NetFwAddPorts(name, port, Protocol.TCP);
      }
      catch (Exception ex)
      {
        Message_TextBlock.Text = ex.Message;
      }
    }

    private void RemoveRol_Click(object sender, RoutedEventArgs e)
    {
      Message_TextBlock.Text = null;
      try
      {
        var protocol = Protocol_ComboBox.SelectedItem as Protocol?;
        if (protocol == null) throw new ArgumentNullException("必须选择协议类型。");
        int port = Int32.Parse(RolPort_TextBox.Text);
        FireWallHelper.NetFwDelApps(port, Protocol.TCP);
      }
      catch (Exception ex)
      {
        Message_TextBlock.Text = ex.Message;
      }
    }

    private void Devmgmt_Click(object sender, RoutedEventArgs e)
    {
      Message_TextBlock.Text = null;
      try
      {
        var pp = new Process
        {
          StartInfo = new ProcessStartInfo("devmgmt.msc")
        };
        pp.Start();
      }
      catch (Exception ex)
      {
        Message_TextBlock.Text = ex.Message;
      }
    }
  }
}